package com.ky.pro.java.nio.http.server;

import com.ky.pro.java.nio.http.server.filter.FilterManager;
import com.ky.pro.java.nio.http.server.filter.impl.EnhancedFlagHeaderFilter;
import com.ky.pro.java.nio.http.server.filter.impl.GreetingsHeaderFilter;
import com.ky.pro.java.nio.http.server.outbound.client.IGatewayClient;
import com.ky.pro.java.nio.http.server.outbound.client.impl.netty.NettyHttpClientImpl;
import com.ky.pro.java.nio.http.server.outbound.client.impl.ok.http.OkHttpClientImpl;
import com.ky.pro.java.nio.http.server.router.GatewayRouter;
import com.ky.pro.java.nio.http.server.router.IGatewayRouterRule;
import com.ky.pro.java.nio.http.server.router.ServiceRegistrar;
import com.ky.pro.java.nio.http.server.router.impl.RandomRouterRule;

import java.net.InetSocketAddress;

/**
 * @ClassName NettyHttpGatewayMain
 * @Description TODO
 * @Date 2021/11/13 22:55
 * @Author kevin
 **/
public class NettyHttpGatewayMain {

    public static void main(String[] args) throws InterruptedException {

//        IGatewayClient gatewayClient = new OkHttpClientImpl();
        IGatewayClient gatewayClient = new NettyHttpClientImpl();

        FilterManager filterManager = new FilterManager();
        filterManager.addInboundFilter(new GreetingsHeaderFilter());
        filterManager.addOutboundFilter(new EnhancedFlagHeaderFilter());

        ServiceRegistrar serviceRegistrar = new ServiceRegistrar();
        // 注册示例服务地址
        serviceRegistrar.registerService("hello", new InetSocketAddress("127.0.0.1",8101));
//        serviceRegistrar.registerService("hello", new InetSocketAddress("127.0.0.1",8102));
//        serviceRegistrar.registerService("hello", new InetSocketAddress("127.0.0.1",8103));

        IGatewayRouterRule routerRule = new RandomRouterRule();
        GatewayRouter gatewayRouter = new GatewayRouter(routerRule, serviceRegistrar);

        NettyHttpHandler nettyHttpHandler = new NettyHttpHandler(gatewayClient, gatewayRouter, filterManager);

        NettyInboundServer server = new NettyInboundServer(nettyHttpHandler);

        server.start();

        gatewayClient.shutdown();
    }

}
